package com.youngjun.auth.security;

import com.youngjun.auth.pojo.AuthUser;
import com.youngjun.common.security.exception.CustomAuthenticationException;
import com.youngjun.common.security.service.UserDetailsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;

/**
 * @author admin
 */
@Service
public class UserDetailsServiceImpl implements UserDetailsService {

    @Autowired
    PasswordEncoder passwordEncoder;

    @Autowired
    private HttpServletRequest request;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        AuthUser authUser = new AuthUser(
                "1",
                passwordEncoder.encode("1"),
                Arrays.asList(new SimpleGrantedAuthority("/**"))
        );
        String clientId = request.getParameter("client_id");
        authUser.setId(1L);
        authUser.setClientId(clientId);
        return authUser;
    }

    @Override
    public String loadCertificateByUsername(String username) {
        throw new CustomAuthenticationException("证书已过期");
    }
}
